const loginForm = document.getElementById('loginForm');
loginForm.addEventListener('submit', function (e) {
    e.preventDefault();
    const username = document.getElementById('username').value;
    const password = document.getElementById('password').value;
    fetch('/api/login', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        body: `username=${username}&password=${password}`
    })
        .then(response => response.json())
        .then(data => {
            if (data && data.message === "0") {
                alert("登录成功");
                window.location.href = 'manage.html';
            } else if(data && data.message === "1"){
                alert("用户名或密码错误");
                console.log("用户名或密码错误");
            } else if(data && data.message === "2")
            {
                alert("用户未注册，请先注册账户");
                console.log("用户未注册，请先注册账户");
            }
        })
        .catch(error => console.log(error));
});

// 为新添加的按钮添加点击事件
const otherPageButton = document.getElementById('otherPageButton');
otherPageButton.addEventListener('click', function () {
    window.location.href = 'login.html';
});